﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Whir.Common.WebHelper;

public partial class Whir_System_Module_Information_InfoType : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        RoleHelper.IsLogin();
        if (!IsPostBack)
        {
            if (Request.QueryString["TypeID"] != null)
            {
                TypeID = NetRequest.SafeRequest(Page, "TypeID");
            }
            if (TypeID == "") return;

            ChangeMode(Mode.Create);

            BindTree(TypeID);

            BindDrop();
        }
    }
    /// <summary>
    /// 绑定下拉
    /// </summary>
    private void BindDrop()
    {
        string SqlTemp = "";
        DropDownList1.Items.Clear();
        DropDownList1.DataTextField = "TypeName";
        DropDownList1.DataValueField = "TypeID";
        Whir.Common.BindHelper.BindDropDownList(DropDownList1, "InfoType", TypeID, "");
        SqlTemp = "select * from InfoType where TypeID = " + TypeID + "";
        DataTable dt = Whir.SQLServerDAL.DbHelperSQL.Query(SqlTemp).Tables[0];
        if (dt.Rows.Count > 0)
        {
            DropDownList1.Items.Insert(0, new ListItem(dt.Rows[0]["TypeName"].ToString(), dt.Rows[0]["TypeID"].ToString()));
        }
    }
    #region 属性


    public enum Mode
    {
        /// <summary>
        /// 增加模式
        /// </summary>
        Create,
        /// <summary>
        /// 修改模式
        /// </summary>
        Update
    }

    /// <summary>
    /// 切换模式
    /// </summary>
    /// <param name="mode"></param>
    private void ChangeMode(Mode mode)
    {
        switch (mode)
        {
            case Mode.Create:
                Button1.Visible = false;
                Button3.Visible = false;
                Button2.Visible = true;
                Button4.Visible = false;
                break;
            case Mode.Update:
                Button1.Visible = true;
                Button3.Visible = true;
                Button2.Visible = false;
                Button4.Visible = true;
                break;
        }
    }


    /// <summary>
    /// 当前TypeID
    /// </summary>
    public string TypeID
    {
        get
        {
            string _TypeID = "";
            if (this.ViewState["TypeID"] != null)
            {
                _TypeID = ViewState["TypeID"].ToString();
            }
            return _TypeID;
        }
        set { this.ViewState["TypeID"] = value; }
    }

    /// <summary>
    /// 绑定树控件

    /// </summary>
    private void BindTree(string Type_ParentID)
    {
        Whir.BLL.InfoType bll = new Whir.BLL.InfoType();
        Whir.Model.InfoType model = bll.GetModel(int.Parse(Type_ParentID));

        TreeView1.Nodes.Clear();
        TreeNode node = new TreeNode();
        node.Text = model.TypeName;
        node.Value = model.TypeID.ToString();
        node.PopulateOnDemand = false;
        node.SelectAction = TreeNodeSelectAction.Expand;
        node.Expanded = true;
        TreeNode nodeItem;

        Whir.BLL.InfoType bll2 = new Whir.BLL.InfoType();
        DataSet ds = bll2.GetChild(Type_ParentID);

        if (ds == null) return;

        for (int i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
        {
            nodeItem = new TreeNode();
            nodeItem.Text = ds.Tables[0].Rows[i]["TypeName"].ToString();
            nodeItem.Value = ds.Tables[0].Rows[i]["TypeID"].ToString();
            nodeItem.PopulateOnDemand = false;
            nodeItem.Expanded = true;
            nodeItem.SelectAction = TreeNodeSelectAction.SelectExpand;
            BindNode(nodeItem, ds.Tables[0].Rows[i]["TypeID"].ToString());
            node.ChildNodes.Add(nodeItem);
        }
        TreeView1.Nodes.Add(node);
    }

    /// <summary>
    /// 递归绑定结点
    /// </summary>
    /// <param name="node">节点</param>
    /// <param name="ParentID">父ID</param>
    private void BindNode(TreeNode node, string ParentID)
    {
        TreeNode nodeItem;

        Whir.BLL.InfoType bll = new Whir.BLL.InfoType();
        DataSet ds = bll.GetChild(ParentID);

        if (ds == null) return;
        if (ds.Tables[0].Rows.Count == 0) return;

        for (int i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
        {
            nodeItem = new TreeNode();
            nodeItem.Text = ds.Tables[0].Rows[i]["TypeName"].ToString();
            nodeItem.Value = ds.Tables[0].Rows[i]["TypeID"].ToString();
            //nodeItem.PopulateOnDemand = false;
            nodeItem.SelectAction = TreeNodeSelectAction.Select;
            BindNode(nodeItem, ds.Tables[0].Rows[i]["TypeID"].ToString());
            node.ChildNodes.Add(nodeItem);
        }
    }
    #endregion

    /// <summary>
    /// 树事件
    /// </summary>
    protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
    {
        Reset();
        //删除自身
        int k = 1;
        while (k < DropDownList1.Items.Count)
        {
            if (DropDownList1.Items[k].Value == TreeView1.SelectedNode.Value)
            {
                DropDownList1.Items.RemoveAt(k);
                break;
            }
            k++;
        }
        //初始化

        DropDownList1.SelectedValue = TreeView1.SelectedNode.Parent.Value;
        TextBox1.Text = TreeView1.SelectedNode.Text;

        ChangeMode(Mode.Update);
    }
    /// <summary>
    /// 初始化

    /// </summary>
    private void Reset()
    {
        TextBox1.Text = "";
        BindDrop();
        DropDownList1.SelectedIndex = -1;
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        if (this.TextBox1.Text.Trim() == "")
        {
            JsHelper.Alert(Page, "请输入类别名称！");
            this.TextBox1.Focus();
            return;
        }

        Whir.BLL.InfoType bll = new Whir.BLL.InfoType();
        Whir.Model.InfoType model = new Whir.Model.InfoType();

        model.TypeName = TextBox1.Text;
        model.ParentID = int.Parse(DropDownList1.SelectedItem.Value);
        model.Sort = 1;
        if (bll.Add(model) > 0)
        {
            JsHelper.Alert(Page, "增加成功!");
            Reset();
            BindTree(TypeID);
        }
        else
        {
            JsHelper.Alert(Page, "增加失败!");
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Whir.BLL.InfoType bll = new Whir.BLL.InfoType();
        Whir.Model.InfoType model = new Whir.Model.InfoType();

        model.TypeID = int.Parse(TreeView1.SelectedNode.Value);
        model.TypeName = TextBox1.Text;
        model.ParentID = int.Parse(DropDownList1.SelectedItem.Value);
        model.Sort = 1;
        bll.Update(model);
        JsHelper.Alert(Page, "修改成功!");
        BindTree(TypeID);
        Reset();
        ChangeMode(Mode.Create);
    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        Whir.BLL.InfoType bll = new Whir.BLL.InfoType();
        this.dt = bll.GetList().Tables[0];

        //递归删除其所有子类别
        DeleteType(int.Parse(TreeView1.SelectedNode.Value), bll);

        BindTree(TypeID);
        JsHelper.Alert(Page, "删除成功!");
        Reset();
        ChangeMode(Mode.Create);
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        Reset();
        ChangeMode(Mode.Create);
        BindTree(TypeID);
    }
    //全局类别列表
    private DataTable dt;

    /// <summary>
    /// 递归删除子分类

    /// </summary>
    /// <param name="pid">父ID</param>
    /// <param name="bll_Info_Type">modle</param>
    private void DeleteType(int pid, Whir.BLL.InfoType bll_Info_Type)
    {
        //删除父类别

        bll_Info_Type.Delete(pid);
        //获取其直接子分类
        DataRow[] drs = this.dt.Select("ParentID=" + pid);
        //递归
        foreach (DataRow dr in drs)
        {
            DeleteType(Convert.ToInt32(dr["TypeID"].ToString()), bll_Info_Type);
        }
    }
}
